vec2 fixuv(in vec2 uv) {

    vec2 p = uv / iResolution.xy;
    p.x *= iResolution.x / iResolution.y;
    p.y = -(p.y - 1.);
    p *= 10.;
    // vec2 p = 3. * (2. * uv - iResolution.xy) / min(iResolution.x, iResolution.y);
    return p;
}

// 画圆 当前像素，绘制的中心点位置，半径，颜色，背景颜色
vec3 drawCircle(in vec2 p, in vec2 pos, in float r, in vec3 color, in vec3 col) {
    col = mix(col, color, smoothstep(r, r * 0.95, length(p - pos)));
    return col;
}

vec3 drawSegment(in vec2 p, in vec2 sp, in vec2 ep, in float w, in vec3 color, in vec3 col) {

    vec2 v1 = ep - sp;
    vec2 v2 = sp - ep;
    vec2 a1 = p - sp;
    vec2 a2 = p - ep;

    if(dot(v1, a1) > 0.00001 && dot(v2, a2) > 0.00001) {
        float theta = dot(v1, a1) / dot(v1, v1);
        vec2 b = v1 * theta;
        vec2 c = a1 - b;
        float dd = length(v1);
        float d = length(b);
        float f = smoothstep(1., 0.2, 0.1 + 2. * abs(d / dd - 0.5));
        w = w / 2.;
        col = mix(col, color, smoothstep(w, w * 0.95, length(c)));
    }

    return col;
}

vec3 drawEllipse(in vec2 p, in vec2 pos, in float w, in float h, in vec3 color, in vec3 col) {

    w = w / 2.;
    h;
    vec2 sp = vec2(pos.x - w, pos.y);
    vec2 ep = vec2(pos.x + w, pos.y);

    vec2 v1 = ep - sp;
    vec2 v2 = sp - ep;
    vec2 a1 = p - sp;
    vec2 a2 = p - ep;

    if(dot(v1, a1) > 0.00001 && dot(v2, a2) > 0.00001) {
        float theta = dot(v1, a1) / dot(v1, v1);
        vec2 b = v1 * theta;
        vec2 c = a1 - b;
        // 
        float dd = length(v1);
        // 底边长度
        float d = length(b);

        float f = 1. - abs(d / dd - 0.5) * 2.;
        f = abs(1. - f * f);
        h *= f;
        col = mix(col, color, smoothstep(h, h * 0.95, length(c)));
    }

    return col;
}

vec3 drawEllipse2(in vec2 p, in vec2 f1, in vec2 f2, in vec3 color, in vec3 col) {

    float c2 = length(f1 - f2);
    float a1 = length(p - f1);
    float a2 = length(p - f2);

    float b1= a1+a2-c2;
    float h = 1./c2;
    col = mix(col, color, smoothstep(h, h*0.99 ,b1));

    return col;
}

// 画网格
vec3 drawGrid(in vec2 p) {
    float f = floor(p.x) + floor(p.y);
    vec3 col = mix(vec3(0.75), vec3(0.3), smoothstep(0., 1., mod(f, 2.)));

    return col;
}

// 画中心线
vec3 drawCenterLine(in vec2 p, in vec3 col) {

    p = (2.0 * p - iResolution.xy) / min(iResolution.x, iResolution.y);
    col = mix(col, vec3(1., 0., 0.), smoothstep(abs(p.x), abs(p.x), fwidth(p.x) * 2.));
    col = mix(col, vec3(0., 0., 1.), smoothstep(abs(p.y), abs(p.y), fwidth(p.y) * 2.));
    return col;
}

void mainImage(out vec4 fragColor, in vec2 fragCoord) {
    vec2 p = fixuv(fragCoord);
    float w = fwidth(p.x);
    vec3 col = drawGrid(p);
    col = drawCenterLine(fragCoord, col);
    col = drawCircle(p, vec2(1., 1.), 0.5, vec3(1.), col);
    col = drawSegment(p, vec2(2., 1.), vec2(6., 4.), w * 100., vec3(1., 0., 0.8), col);
    col = drawEllipse(p, vec2(3., 8.), 2., 1., vec3(0.2, 0.8, 0.3), col);

    vec2 f1 = vec2(1., 4.);
    vec2 f2 = vec2(5., 4.);
    col = drawEllipse2(p, f1, f2, vec3(1.), col);

    fragColor = vec4(col, 1.);
}
